main(n,i,w,b,l)
{
	char c[10001];
	char r[5000];

	for(gets(c);gets(c);puts(b?"NO":"YES"))
	{
		l=strlen(c);
		for(b=w=i=0;i<l&&!b;i++)
		{
			if(c[i]=='(')r[w++]=1;
			if(c[i]=='{')r[w++]=2;
			if(c[i]=='[')r[w++]=3;
			b|=w?0:1;
			if(c[i]==']')b|=r[--w]!=3;
			if(c[i]=='}')b|=r[--w]!=2;
			if(c[i]==')')b|=r[--w]!=1;

		}
	}
}
